package com.xyz.rok.tools.service.impl;

import com.xyz.rok.tools.constant.Integral;
import com.xyz.rok.tools.constant.SystemCode;
import com.xyz.rok.tools.exception.TitleException;
import com.xyz.rok.tools.exception.UnionException;
import com.xyz.rok.tools.mapper.ApplyTitleMapper;
import com.xyz.rok.tools.mapper.TitleMapper;
import com.xyz.rok.tools.mapper.UserMapper;
import com.xyz.rok.tools.model.ApplyTitle;
import com.xyz.rok.tools.model.Title;
import com.xyz.rok.tools.model.User;
import com.xyz.rok.tools.model.request.TitleRequest;
import com.xyz.rok.tools.service.TitleQueueService;
import com.xyz.rok.tools.service.TitleService;
import com.xyz.rok.tools.service.WxAppWrapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TitleServiceImpl implements TitleService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private ApplyTitleMapper applyTitleMapper;
    @Resource
    private TitleMapper titleMapper;
    @Autowired
    private TitleQueueService titleQueueService;
    @Autowired
    private WxAppWrapperService wxAppWrapperService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyTile(TitleRequest titleRequest) throws Exception {
        Title title = titleMapper.selectByServerId(titleRequest.getServerId());
        if (title == null) {
            throw new TitleException(SystemCode.TITLE_NO_ERROR);
        }
        if (titleRequest.getPassword().equals(title.getPassword())) {
            User user = new User();
            user.setId(titleRequest.getUserId());
            user.setServerId(titleRequest.getServerId());
            user.setServerCode(titleRequest.getServerCode());
            user.setPassword(titleRequest.getPassword());
            user.setCoordinateX(titleRequest.getCoordinateX());
            user.setCoordinateY(titleRequest.getCoordinateY());
            userMapper.updateByPrimaryKeySelective(user);
            ApplyTitle applyTitleDb = applyTitleMapper.selectByServerIdAndUserId(titleRequest.getServerId(), titleRequest.getUserId());
            if (applyTitleDb == null) {
                ApplyTitle applyTitle = new ApplyTitle();
                applyTitle.setTitle(titleRequest.getTitle());
                applyTitle.setCreateTime(new Date());
                applyTitle.setCoordinateX(titleRequest.getCoordinateX());
                applyTitle.setCoordinateY(titleRequest.getCoordinateY());
                applyTitle.setServerId(titleRequest.getServerId());
                applyTitle.setServerCode(titleRequest.getServerCode());
                applyTitle.setUserId(titleRequest.getUserId());
                applyTitleMapper.insertSelective(applyTitle);
                applyTitle.setId(applyTitle.getId());
                //titleQueueService.addQueue(titleRequest.getTitle(), titleRequest.getServerId(), applyTitle);
            } else {
                throw new TitleException(SystemCode.TITLE_HAS_APPLY);
            }
        } else {
            throw new TitleException(SystemCode.TITLE_APPLY_PASSWORD_ERROR);
        }

    }

    @Override
    public ApplyTitle selectByUserId(Long userId) {
        return applyTitleMapper.selectByUserId(userId);
    }

    @Override
    public List<ApplyTitle> selectByServerId(Integer serverId) {
        return applyTitleMapper.selectByServerId(serverId);
    }

    @Override
    public void checkingTitle(TitleRequest titleRequest) throws Exception {
        Title title = titleMapper.selectByServerId(titleRequest.getServerId());
        if (title == null) {
            throw new TitleException(SystemCode.TITLE_NO_ERROR);
        }
        if (titleRequest.getPassword().equals(title.getPassword())) {
            User user = new User();
            user.setId(titleRequest.getUserId());
            user.setServerId(titleRequest.getServerId());
            user.setPassword(titleRequest.getPassword());
            user.setNickName(titleRequest.getNickName());
            user.setHeadImg(titleRequest.getHeadImg());
            userMapper.updateByPrimaryKeySelective(user);
        } else {
            throw new TitleException(SystemCode.TITLE_APPLY_PASSWORD_ERROR);
        }
    }

    @Override
    public int updateGetTitleTime(Long id) throws Exception {
        ApplyTitle title = new ApplyTitle();
        title.setId(id);
        title.setGetTime(new Date());
        return applyTitleMapper.updateByPrimaryKeySelective(title);
    }

    @Override
    public void checkingTitleAdmin(TitleRequest titleRequest) throws Exception {
        Title title = titleMapper.selectByServerId(titleRequest.getServerId());
        if (title == null) {
            throw new TitleException(SystemCode.TITLE_NO_ERROR);
        }
        if (!titleRequest.getAdminPassword().equals(title.getAdminPassword())) {
            throw new TitleException(SystemCode.TITLE_ADMIN_PASSWORD_ERROR);
        }
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return titleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Title record) {
        return titleMapper.insert(record);
    }

    @Override
    public int insertSelective(Title record) {
        return titleMapper.insertSelective(record);
    }

    @Override
    public Title selectByPrimaryKey(Integer id) {
        return titleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Title record) {
        return titleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Title record) {
        return titleMapper.updateByPrimaryKey(record);
    }

    @Override
    public Title selectBySerialNumber(String serialNumber) {
        return titleMapper.selectBySerialNumber(serialNumber);
    }

    @Override
    public Title selectTitleByServerId(Integer serverId) {
        return titleMapper.selectTitleByServerId(serverId);
    }

    @Override
    public void updateTitleByServerId(Title record) {
        record.setUpdateTime(new Date());
        titleMapper.updateTitleByServerId(record);
    }

    @Override
    public ApplyTitle getFirstTitle(Integer title, Integer serverId) {
        ApplyTitle firstTitle = applyTitleMapper.getFirstTitle(title, serverId);
        if (firstTitle != null) {
            wxAppWrapperService.pushQueueMsg(firstTitle.getUserId(), firstTitle.getTitle());
        }
        return firstTitle;
    }

    @Override
    public int getCountByServerId(Integer serverId) {
        return applyTitleMapper.getCountByServerId(serverId);
    }
}

